package com.project.frba.bi.loading;

import com.project.frba.bi.descriptor.Dimension;
import com.project.frba.bi.transformation.FactTable;

public class FactTableBuilder {

	private FactTable factTable;
	private String statement; 
	
	public FactTableBuilder(FactTable factTable){
		this.factTable = factTable;
	}
	
	public void buildCreateTableStatement(){
		StringBuffer statement = new StringBuffer();
		statement.append("CREATE TABLE " + factTable.getDescriptor().getDescriptorName() + "facttable_");
		//getFacttable trae el nombre de la tabla
		statement.append(this.factTable.getDescriptor().getFacttable());
		statement.append("(");
		statement.append("id BIGINT(20) DEFAULT NULL AUTO_INCREMENT, ");
		statement.append("metric INTEGER, ");
		
		for(Dimension dimension : this.factTable.getDescriptor().getDimensions()){
			statement.append(dimension.getName() + "dimension");
			statement.append(" ");
			statement.append("BIGINT(20), ");
		}
		
		for(Dimension dimension: this.factTable.getDescriptor().getDimensions()){
			statement.append("FOREIGN KEY (" + dimension.getName() + "dimension) REFERENCES " +	this.factTable.getDescriptor().getDescriptorName() +  dimension.getName() + "dimension(id), ");			
		}
		
		statement.append("PRIMARY KEY (id)) ENGINE InnoDB;");
		this.statement=statement.toString().trim();
	}
	
	public String getCreateFactTableStatement(){
		return this.statement;
	}
	
	public String getDropTableStatement(){
		return "DROP TABLE IF EXISTS " + factTable.getDescriptor().getDescriptorName() + "facttable_" + this.factTable.getDescriptor().getFacttable() + ";";
	}
}
